package com.lijia.ffmpeg4android;

import com.lijia.ffmpeg4android.fragment.CrimeFragment;
import com.lijia.ffmpeg4android.fragment.CrimeFragment.CrimeFragmentDelegate;
import com.lijia.ffmpeg4android.fragment.CrimeListFragment;
import com.lijia.ffmpeg4android.fragment.CrimeListFragment.CrimeListFragmentDelegate;
import com.lijia.ffmpeg4android.model.Crime;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class CrimeListActivity extends SingleFragmentActivity 
	implements CrimeListFragmentDelegate,CrimeFragmentDelegate {

	@Override
	protected Fragment createFragment() {
		return new CrimeListFragment();
	}
	
	@Override	
	protected int getLayoutResId() {
		return R.layout.activity_twopane;
	}

	@Override
	public void OnCrimeItemSelected(Crime crime) {
		if (findViewById(R.id.fragmentDetailContainer) == null) {
			Intent i = new Intent(this,CrimePagerActivity.class);
			i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId());
			startActivity(i);
		} else {
			FragmentManager fm = getSupportFragmentManager();
			FragmentTransaction ft = fm.beginTransaction();
			
			Fragment oldDetail = fm.findFragmentById(R.id.fragmentDetailContainer);
			Fragment newDetail = CrimeFragment.newInstance(crime.getId());
			if (oldDetail != null) {
				ft.remove(oldDetail);
			}
			ft.add(R.id.fragmentDetailContainer,newDetail);
			ft.commit();
		}
	}

	@Override
	public void onCrimeUpdated(Crime crime) {
		FragmentManager fm = getSupportFragmentManager();
		CrimeListFragment listFragment = (CrimeListFragment)fm.findFragmentById(R.id.fragmentContainer);
		listFragment.updateUI();
	}
}
